// caveOsgViewer.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <osg/Group>
#include <osgviewer/viewer>
#include <osg/Camera>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include <iostream>

#include "CaveViewer.h"

int _tmain(int argc, _TCHAR* argv[])
{
	std::string scenePath;
	CCaveViewer::CaveFaceId caveFaceId = CCaveViewer::CAVE_CENTER;
	double eyeDistance = 0.065;
	int viewportShift = 60;
	osg::Vec3 startEye(0,0,0);
	double caveWidth = 3;
	double caveHeight = 3;
	double caveDepth = 3;
	double groundBottom = 3;
	double groundTop = 3;
	double scaleX = 1.0;
	double scaleY = 1.0;
	double scaleZ = 1.0;
	osg::Vec3 lightPos(0,0,0);
	bool pcMode;

	// read config file
	std::ifstream stream;
	stream.open("moveConfig.txt");
	std::string tmp;
	do
	{
		stream >> tmp;

		if(tmp == "scene")
			stream >> scenePath;
		else if(tmp == "face")
		{
			int i;
			stream >> i;
			caveFaceId = static_cast<CCaveViewer::CaveFaceId>(i);		
		}
		else if(tmp == "eyeDistance")
			stream >> eyeDistance;
		else if(tmp == "viewportShift")
			stream >> viewportShift;
		else if(tmp == "startEye")
		{
			float x, y, z;
			stream >> x >> y >> z;
			startEye = osg::Vec3(x, y, z);
		}
		else if(tmp == "caveDimension")
			stream >> caveWidth >> caveHeight >> caveDepth;
		else if(tmp == "groundDimension")
			stream >> groundBottom >> groundTop;
		else if(tmp == "sceneScale")
			stream >> scaleX >> scaleY >> scaleZ;
		else if(tmp == "lightPosition")
		{
			float x, y, z;
			stream >> x >> y >> z;
			lightPos = osg::Vec3(x, y, z);
		}
		else if(tmp == "pcMode")
		{
			stream >> tmp;
			if(tmp=="true")pcMode=true;
			else pcMode=false;
			std::cout << "PC Mode: "<<pcMode;
		}

	} while(!stream.eof());
	stream.close();

	osg::ref_ptr<osg::Node> scene = osgDB::readNodeFile(scenePath);

	// create viewer
	CCaveViewer viewer;
	viewer.setCaveFaceId(caveFaceId);
	viewer.setEyeDistance(eyeDistance);
	viewer.setStartEye(startEye);
	viewer.setViewportShift(viewportShift);
	viewer.setCaveDimensions(caveWidth, caveHeight, caveDepth);
	viewer.setGroundDimensions(groundTop, groundBottom);
	viewer.setSceneScale(scaleX, scaleY, scaleZ);
	viewer.setLightPos(lightPos);
	viewer.init();
	viewer.setScene(scene.get());

	viewer.realize();
	while(!viewer.done())
	{
		viewer.frame();		
	}

	return 0;
}

